package com.toocms.jinjuhang.ui.mine.account_management

import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.RelativeSizeSpan
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.google.android.material.tabs.TabLayout
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.databinding.FgtAccountManagementBinding
import com.toocms.jinjuhang.ui.base.BaseFgt
import kotlin.math.abs

class AccountManagementFgt : BaseFgt<FgtAccountManagementBinding, AccountManagementModel>() {
    override fun onFragmentCreated() {
        topBar.visibility = View.GONE
        binding.appBar.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
            val canOffsetHeight =
                appBarLayout.height - binding.filterTl.height - binding.toolbar.height
            var percentage = abs(verticalOffset).toFloat() / abs(canOffsetHeight).toFloat()
            if (1f < percentage) percentage = 1f
            val clrMain = ColorUtils.getColor(R.color.clr_main)
            val titleBackgroundColor = Color.argb(
                (255 * percentage).toInt(),
                Color.red(clrMain),
                Color.green(clrMain),
                Color.blue(clrMain)
            )
            binding.toolbar.setBackgroundColor(titleBackgroundColor)
        }
        binding.filterTl.apply {
            addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    tab?.apply {
                        val textSpan = SpannableStringBuilder(text.toString())
                        textSpan.setSpan(
                            RelativeSizeSpan(1.2f),
                            0,
                            textSpan.length,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                        text = textSpan
                    }
                    viewModel.symbol = tab?.tag?.toString() ?: "0"
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.apply {
                        text = text.toString()
                    }
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }

            })
            addTab(
                newTab().setText(StringUtils.getString(R.string.str_billing_details)).setTag("0")
            )
            addTab(
                newTab().setText(StringUtils.getString(R.string.str_disbursement_details))
                    .setTag("2")
            )
//            addTab(
//                newTab().setText(StringUtils.getString(R.string.str_recharge_details)).setTag("1")
//            )
        }
    }

    override fun getLayoutResId() = R.layout.fgt_account_management

    override fun getVariableId() = BR.accountManagementModel

    override fun viewObserver() {
        viewModel.stopRefreshOrLoadSingleLiveEvent.observe(this) {
            binding.refreshSrl.finishRefresh()
            binding.refreshSrl.finishLoadMore()
        }
    }
}